package com.xky.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xky.mapper.PublishMapper;
import com.xky.pojo.PageBean;
import com.xky.pojo.Publish;
import com.xky.pojo.PublishBean;
import com.xky.pojo.Result;
import com.xky.service.PublishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class PublishServiceImpl implements PublishService {

    @Autowired
    private PublishMapper publishMapper;
    /**
     * 添加出版社信息
     * @param publish
     */
    @Override
    public void addPublish(@RequestBody Publish publish) {
        publish.setCreateTime(LocalDateTime.now());
        publishMapper.insert(publish);
    }

    /**
     * 删除出版社信息
     * @param publish
     */
    @Override
    public void deletePublish(Publish publish) {
        publishMapper.deleteById(publish.getPublishId());
    }

    /**
     * 更新出版社信息
     * @param publish
     */
    @Override
    public void updatePublishById(Publish publish) {
        publishMapper.updateById(publish);
    }

    /**
     * 批量删除出版社信息
     * @param ids
     */
    @Override
    public void deleteIds(List<Integer> ids) {
        publishMapper.deleteBatchIds(ids);
    }

    /**
     * 根据ID查询出版社信息
     * @param id
     * @return
     */
    @Override
    public Publish selectById(Integer id) {
        return publishMapper.selectById(id);
    }

    /**
     * 查询全部的出版社信息
     * @return
     */
    @Override
    public List<Publish> selectAll() {
        return publishMapper.selectList(Wrappers.emptyWrapper());
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param publishName
     * @return
     */
    @Override
    public PublishBean pages(Integer page, Integer pageSize, String publishName) {
        //1、创建分页对象
        IPage<Publish> publishIPage =new Page<>(page,pageSize);

        //2、构建查询条件
        LambdaQueryWrapper<Publish> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (publishName!=null && publishName.isEmpty()){
            objectLambdaQueryWrapper.like(Publish::getPublishName,publishName);
        }

        //3、执行分页查询
        IPage<Publish> result = publishMapper.selectPage(publishIPage, objectLambdaQueryWrapper);

        //4、封装自定义分页对象
        return new PublishBean(result.getTotal(),result.getRecords());
    }
}
